#!/usr/bin/env python
# -*- coding: utf-8 -*-
# ======================================================================
#    Copyright (C) 2006 Robert Muth <robert at muth dot org>
#
#    This program is free software; you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation; version 2 of June 1991.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program in form of the file COPYING;
#    if not, write to the
#
#    Free Software Foundation, Inc. <http://www.fsf.org>
#    59 Temple Place, Suite 330,
#    Boston, MA 02111-1307  USA
#    $Id: player_unittest.py 255 2006-10-13 01:16:17Z muth $
#
# ======================================================================

"""

"""

# pythom imports
import unittest
import logging
import  md5

# local imports
import player
import song
import sound
# ======================================================================

def HexDigest(s):
    m = md5.md5()
    m.update(s)
    return m.hexdigest()

class testSong(unittest.TestCase):

    def setUp(self):
        #print "before test"
        self._channel = sound.Channel()
        return

    def tearDown(self):
        #print "after test"
        return

    def testRecordingPlayer(self):
        s = song.Song()
        file_player = player.RecordingPlayer()
        filename = "Test/rumba.sng"
        drumdir = "Test"
        s.deserialize(file(filename).read(), drumdir)
        ret = player.LoadAllSongDrumSamples(s)
        self.assertEqual(ret, "")
        #PlaySong(audio_player, s)


        logging.info("testing pattern")
        file_player.reset()
        player.PlayPattern(file_player, s, 0)
        data = file_player.get_wave()
        logging.info("data size %d" , len(data))

        golden_pattern = file("Test/rumba.patt0.wav", "rb").read()
        # open("rumba.patt0.wav", "wb").write(data)
        self.assertEqual(HexDigest(golden_pattern), HexDigest(data))


        logging.info("testing short range")
        file_player.reset()
        player.PlaySong(file_player, s, 0, 0)
        data = file_player.get_wave()
        logging.info("data size %d", len(data))
        self.assertEqual(HexDigest(golden_pattern), HexDigest(data))

        logging.info("testing long range")
        file_player.reset()
        player.PlaySong(file_player, s, 0, 1)
        data = file_player.get_wave()
        logging.info("data size %d", len(data))

        golden_song = file("Test/rumba.song.wav", "rb").read()
        #open("rumba.song.wav", "wb").write(data)
        self.assertEqual(HexDigest(golden_song), HexDigest(data))

        logging.info("testing song")
        file_player.reset()
        player.PlaySong(file_player, s)
        data = file_player.get_wave()
        logging.info("data size %d", len(data))
        self.assertEqual(HexDigest(golden_song), HexDigest(data))

        return


if __name__ == '__main__':
    logging.basicConfig(level=logging.INFO)
    suite = unittest.TestSuite()
    suite.addTest(unittest.makeSuite(testSong))
    unittest.TextTestRunner(verbosity=2).run(suite)

